package org.ifi.xechecs.xboard;

import java.io.InputStreamReader;
//import org.apache.logging.log4j.*;
import org.ifi.xechecs.ai.Search;
import org.ifi.xechecs.board.Board;
import org.ifi.xechecs.board.Move;
import org.ifi.xechecs.board.Piece;


public class ChessBoard{
	private int moveCount = 0;
	private static final int MAX_DEPTH = 4;
	private Board board = new Board();
	private boolean forceModeOn;
	private Move move;

	private static final String[] KEYWORDS = {"xboard", "protover", "accepted",
			"rejected", "new", "variant", "quit", "random", "force", "go", 
			"playother", "white", "black", "level", "st", "sd", "nps", "time", 
			"otim", "usermove", "?", "ping", "draw", "result", "setboard", "edit", 
			"hint", "bk", "undo", "remove", "hard", "easy", "post", "nopost", 
			"analyze", "name X", "rating", "ics HOSTNAME", "computer", "pause", 
			"resume", "memory", "cores", "egtpath", "option", "post", "print"};
	public void run() {
        try {
            InputStreamReader reader = new InputStreamReader(System.in);
            String curS = "";
            while (true) {
                int c = reader.read();
                if (c != -1) {
                    curS += (char) c;
                }
                if (c == '\n') {
                    process(curS);
                    curS = "";
                }
            }
        }
        catch (Exception e) { 
            e.printStackTrace(System.out);
        }		
	}
	
	
    public static void main (String[] args){
        ChessBoard cb = new ChessBoard();
        cb.run();
    }

    private void doMove(){
    	Thread t = new Thread(){
    		@Override
    		public void run(){
    	    	Search search;
    	    	search = new Search(board, MAX_DEPTH, Piece.WHITE);
    	    	move = search.getBestMove();   			
    		}
    	};
    	
    	try {
    		t.start();
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	if(move == null){
        	System.out.println("resign");
    	}else{
    		board = board.applyMove(move);
    		System.out.println("move " + move.toString());
    	}    	
System.out.println(board.toString());    	
		System.out.flush();
    }
    
    private void process(String command){
        command = command.trim();
        if (command.length() == 0)
            return;

        // true if it's actually command, not a move.
        boolean isCommand = false;
        for (String s : KEYWORDS)
            if (command.contains(s)) {
                isCommand = true;
                break;
            }

        if (!isCommand) {
            processMove(command);
System.out.println("#ComputerMove:" + command);
System.out.println(board.toString());    	

        	//doMove();
            return;
        }

        if (command.contains("quit"))
            System.exit(0);
        if (command.contains("go")) {
        	
            forceModeOn = false;
//            makeMove();
        	doMove();
            return;
        }
        if (command.contains("new")) {
//            forceModeOn = false;
            board = new Board();
//            board.set
            return; 
        }   
        if (command.contains("force")) {
            forceModeOn = true;
            return;
        }   
        if (command.contains("print")) {
//            System.out.println(board); 
            return;
        }   
        if (command.contains("nopost")) {
//            strategy.setIsThinkingOutputOn(false);
            return;
        }   
        if (command.contains("post")) {
//            strategy.setIsThinkingOutputOn(true);
            return;
        }   
    }


	private void processMove(String command) {
		// Get move from command (x board or people)
		Move move = new Move(command);
		board = board.applyMove(move);
System.out.println("#chessboard enpassant: " + board.getEnPassant());
//System.out.println("#Adversaire#" + command);
//move.g
//System.out.println(board.toString());
	}              
}
